import axios from 'axios'

const request = axios.create({
    baseURL: 'http://localhost:8081/',
    timeout: 30000
})
let token = '';
axios.interceptors.request.use(function (config) {
    let user = JSON.parse(window.sessionStorage.getItem('access-user'));
    if (user) {
        token = user.token;
    }
    config.headers.common['token'] = token;
    return config;
}, function (error) {
    console.info("error: ");
    console.info(error);
    return Promise.reject(error);
});

request.interceptors.response.use(
    response => {
        let res = response.data;
        if (response.config.responseType === 'blob') {
            return res
        }
        if (typeof res === 'string') {
            res = res ? JSON.parse(res) : res
        }
        return res;
    },
    error => {
        console.log('err' + error)
        return Promise.reject(error)
    }
)

export default request;

